package com.sw.small_world.fragment.home.search;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sw.small_world.R;
import com.sw.small_world.activity.ClickButtonActivity;
import com.sw.small_world.activity.MainActivity;
import com.sw.small_world.fragment.base.BaseListFragment;
import com.sw.small_world.utils.CommonUtils;
import com.sw.small_world.utils.ConstantUtils;
import com.sw.small_world.utils.PopUtils;
import com.sw.small_world.utils.StartUtils;
import com.sw.small_world.utils.ToastUtil;
import com.sw.small_world.widgets.CircleImageView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/25.
 */

public class SearchTopicFragment  extends BaseListFragment {

    private ClickButtonActivity activity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (ClickButtonActivity) getActivity();
    }

    @Override
    protected Object requestData() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                StartUtils.startActivityById(activity, ConstantUtils.DETAIL_TOPIC_SQUARE);
            }
        });
        for (int i = 0; i < 1; i++) {
            list.add("" + i);
        }
        return ConstantUtils.STATE_SUCCESSED;
    }

    @Override
    protected View setView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(activity, R.layout.item_topic_square, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.topicSquareGrade.setOnClickListener(this);
        holder.commentOneGrade.setOnClickListener(this);
        holder.commentTwoGrade.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.topic_square_grade:
                handleTopicSquareGrade();
                break;
            case R.id.comment_one_grade:
                handleCommentOneGrade();
                break;
            case R.id.comment_two_grade:
                handleCommentTwoGrade();
                break;
        }
    }

    private void handleCommentOneGrade() {
        new PopUtils().initGradePop(activity, new PopUtils.OnSureGradeListener() {
            @Override
            public void onSure(int grade) {
                ToastUtil.show("评分为：" + grade);
            }
        }).showAsDropDown(activity.popView);
    }

    private void handleCommentTwoGrade() {
        new PopUtils().initGradePop(activity, new PopUtils.OnSureGradeListener() {
            @Override
            public void onSure(int grade) {
                ToastUtil.show("评分为：" + grade);
            }
        }).showAsDropDown(activity.popView);
    }
    private void handleTopicSquareGrade() {
        new PopUtils().initGradePop(activity, new PopUtils.OnSureGradeListener() {
            @Override
            public void onSure(int grade) {
                ToastUtil.show("评分为：" + grade);
            }
        }).showAsDropDown(activity.popView);
    }

    @Override
    protected void setRefresh() {
        new Thread() {
            @Override
            public void run() {
                try {
                    sleep(1000);
                    CommonUtils.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            refreshListView.onRefreshComplete();
                            ToastUtil.show("刷新成功");
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                super.run();
            }
        }.start();
    }

    @Override
    protected void loadMore() {
        new Thread() {
            @Override
            public void run() {
                try {
                    sleep(1000);
                    CommonUtils.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            refreshListView.onRefreshComplete();
                            ToastUtil.show("加载更多成功");
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                super.run();
            }
        }.start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    static class ViewHolder {
        @Bind(R.id.topic_square_portrait)
        CircleImageView topicSquarePortrait;
        @Bind(R.id.topic_square_name)
        TextView topicSquareName;
        @Bind(R.id.topic_square_deep)
        TextView topicSquareDeep;
        @Bind(R.id.topic_square_time)
        TextView topicSquareTime;
        @Bind(R.id.topic_square_title)
        TextView topicSquareTitle;
        @Bind(R.id.topic_square_content)
        TextView topicSquareContent;
        @Bind(R.id.topic_square_see_all)
        TextView topicSquareSeeAll;
        @Bind(R.id.topic_square_grade)
        TextView topicSquareGrade;
        @Bind(R.id.topic_square_collect)
        TextView topicSquareCollect;
        @Bind(R.id.comment_one_name)
        TextView commentOneName;
        @Bind(R.id.comment_one_grade)
        TextView commentOneGrade;
        @Bind(R.id.comment_one_content)
        TextView commentOneContent;
        @Bind(R.id.rl_content_one)
        RelativeLayout rlContentOne;
        @Bind(R.id.comment_two_name)
        TextView commentTwoName;
        @Bind(R.id.comment_two_grade)
        TextView commentTwoGrade;
        @Bind(R.id.comment_two_content)
        TextView commentTwoContent;
        @Bind(R.id.rl_content_two)
        RelativeLayout rlContentTwo;
        @Bind(R.id.ll_comment)
        LinearLayout llComment;
        @Bind(R.id.comment_topic)
        TextView commentTopic;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}